export function browserRedirect() {
  var sUserAgent = navigator.userAgent.toLowerCase();
  var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  var bIsAndroid = sUserAgent.match(/android/i) == "android";
  var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  if (
    bIsIpad ||
    bIsIphoneOs ||
    bIsMidp ||
    bIsUc7 ||
    bIsUc ||
    bIsAndroid ||
    bIsCE ||
    bIsWM
  ) {
    return "notPc";
  } else {
    return "pc";
  }
}

/**
 * 时间转换
 * @param {*} timestamp
 * 返回yyyy-MM-dd HH:mm:ss
 */
export function formatDateTime(timestamp, hashms = true) {
  var date = new Date(timestamp);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  var d = date.getDate();
  d = d < 10 ? "0" + d : d;
  if (!hashms) {
    return y + "-" + m + "-" + d;
  }
  var h = date.getHours();
  h = h < 10 ? "0" + h : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? "0" + minute : minute;
  second = second < 10 ? "0" + second : second;
  return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
}

export function removeHidden() {
  // document.body.style.overflow = "auto"; // 取消body滚动
  document.querySelector("#app").style.overflow = "auto";
}
export function addHidden() {
  // document.body.style.overflow = "hidden"; // 禁止body滚动
  document.querySelector("#app").style.overflow = "hidden";
}

export const handleLocalStorage = {
  //存储
  set(key, value) {
    localStorage.setItem(key, value);
  },
  //取出数据
  get(key) {
    try {
      const value = localStorage.getItem(key);
      if (value === null || value === undefined || value === "") {
        return null;
      }
      return value;
    } catch (err) {
      return null;
    }
  },
  // 删除数据
  remove(key) {
    localStorage.removeItem(key);
  },
};

export const loadImg = (url) => {
  return new Promise((resolve) => {
    const img = new Image();
    img.onload = () => {
      resolve();
    };
    img.src = url;
  });
};

export default {
  formatDateTime,
  removeHidden,
  addHidden,
  handleLocalStorage,
};
